#include "CookingInterface.h"

CookingInterface::CookingInterface(PhysxWorld* wd)
{
	world = wd;
}

CookingInterface::~CookingInterface(void)
{
}

void CookingInterface::ExtractMeshInfoAndCookTriangleMesh(IMesh* mesh,NxStream& stream, bool clearRamInfoAfter)
	{	
		NxVec3 *vertices = (NxVec3*) malloc(sizeof(NxVec3) * mesh->GetVertexCount());
		UINT *indices = mesh->GetIndices();			

		MeshTypeData mdata  =  mesh->GetMeshType();
		switch(mdata)
		{
			case MeshTypeData::PositionTextureNormalTangentBinormal:
			{
				PositionTextureNormalTangentBinormalVertex* vertex;
				vertex = (PositionTextureNormalTangentBinormalVertex*) mesh->GetVertexData();
				assert(vertex);
				for(int i =0; i < mesh->GetVertexCount(); i++)
				{
					vertices[i] = NxVec3(vertex[i].Position.x,vertex[i].Position.y,vertex[i].Position.z);
				}
				break;
			}
			case MeshTypeData::PositionTextureNormalTangentBinormalAnimated:
			{
				PositionTextureNormalTangentBinormalAnimatedVertex* vertex;
				vertex = (PositionTextureNormalTangentBinormalAnimatedVertex*) mesh->GetVertexData();
				assert(vertex);
				for(int i =0; i < mesh->GetVertexCount(); i++)
				{
					vertices[i] = NxVec3(vertex[i].Position.x,vertex[i].Position.y,vertex[i].Position.z);
				}
				break;
			}
			case MeshTypeData::PositionTextureNormal:
			{
				PositionTextureNormalVertex* vertex;
				vertex = (PositionTextureNormalVertex*) mesh->GetVertexData();
				assert(vertex);
				for(int i =0; i < mesh->GetVertexCount(); i++)
				{
					vertices[i] = NxVec3(vertex[i].Position.x,vertex[i].Position.y,vertex[i].Position.z);
				}
				break;
			}
			default:
				assert(false); ////type not yet implemented
		}
		
		NxTriangleMeshDesc triangleDesc;  
		triangleDesc.numVertices            = mesh->GetVertexCount();  
		triangleDesc.numTriangles           = mesh->GetIndexCount() / 3;  
        triangleDesc.pointStrideBytes       = sizeof(NxVec3);  
        triangleDesc.triangleStrideBytes    = 3*sizeof(UINT);  
        triangleDesc.points                 = vertices;  
        triangleDesc.triangles              = indices;  
        triangleDesc.flags                  = NX_MF_FLIPNORMALS;  	

		bool status = CookTriangleMesh(triangleDesc,stream);
		assert(status);		

		delete [] vertices ;

		if(clearRamInfoAfter)
		{
			mesh->ClearRamInfo();
		}
	}
